package com.cmcc.linlaker.ui.upload;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.application.LinhuApp;
import com.cmcc.linlaker.bean.BaseBean;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.mine.MessageDetailActivity;
import com.cmcc.linlaker.ui.upload.bean.CheckingTaskBean;
import com.cmcc.linlaker.ui.upload.bean.UploadQuestionBean;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.ToastUtils;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TaskDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private CheckingTaskBean.CheckingTaskData taskBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_detail);

        BarUtils.initStatusBar(this);

        taskBean = (CheckingTaskBean.CheckingTaskData) getIntent().getSerializableExtra("taskBean");

        initView();
    }

    private TextView text_name, text_type, text_project, text_death_date;
    private TextView text_fzr, text_state, text_desc;
    private EditText edit_feedback_content;
    private TextView text_content;

    public void initView() {

        text_name = findViewById(R.id.text_name);
        text_type = findViewById(R.id.text_type);
        text_project = findViewById(R.id.text_project);
        text_death_date = findViewById(R.id.text_death_date);
        text_content = findViewById(R.id.text_content);

        text_fzr = findViewById(R.id.text_fzr);
        text_state = findViewById(R.id.text_state);
        text_desc = findViewById(R.id.text_desc);

        edit_feedback_content = findViewById(R.id.edit_feedback_content);
        edit_feedback_content.setFilters(new InputFilter[]{inputFilter});

        getTaskDetail();
    }

    InputFilter inputFilter=new InputFilter() {

        Pattern pattern = Pattern.compile("[^a-zA-Z0-9\\u4E00-\\u9FA5_]");
        @Override
        public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
            Matcher matcher=  pattern.matcher(charSequence);
            if(!matcher.find()){
                return null;
            }else{
                ToastUtils.showText(TaskDetailActivity.this,"只能输入汉字,英文，数字");
                return "";
            }

        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        }
    }

    public void back(View view) {
        finish();
    }

    /**
     * 反馈
     */
    public void feedback(View view) {
        String content = edit_feedback_content.getText().toString();

        if (TextUtils.isEmpty(content)) {
            ToastUtils.showText(this, "请输入反馈内容");
        } else {
            feedback(content);
        }
//        Intent intent = new Intent(TaskDetailActivity.this, FeedbackActivity.class);
//        intent.putExtra("taskDetailData", taskDetailData);
//        startActivityForResult(intent, 100);
    }

    /**
     * 反馈
     */
    private void feedback(String content) {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        String url = ApiDefine.FEEDBACK_NEW;
        Map<String, String> params = new HashMap<>();
        params.put("contant", content);
        params.put("status", "2");
        params.put("id", taskDetailData.id);

        helper.sendPOSTRequest(url, BaseBean.class, params, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    BaseBean bean = (BaseBean) data;
                    if (bean.code.equals("0")) {
                        ToastUtils.showText(TaskDetailActivity.this, "反馈成功");
                        setResult(100);
                        finish();
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(TaskDetailActivity.this);
                    } else {
                        ToastUtils.showText(TaskDetailActivity.this, bean.msg);
                    }
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 100) {
            setResult(100);
            finish();
        }
    }

    private CheckingTaskBean.CheckingTaskData taskDetailData;

    /**
     * 获取详情
     */
    private void getTaskDetail() {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        String url = ApiDefine.TASK_DETAIL + taskBean.id;
        helper.sendGETRequest(url, CheckingTaskBean.CheckingTaskDetailData.class, false, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    CheckingTaskBean.CheckingTaskDetailData bean = (CheckingTaskBean.CheckingTaskDetailData) data;
                    if (bean.code.equals("0")) {
                        CheckingTaskBean.CheckingTaskData detailData = bean.data;
                        taskDetailData = bean.data;

                        text_name.setText(detailData.name);
                        text_type.setText("任务类型：" + getProjectTypeStr(detailData.type));
                        text_project.setText("所属项目：" + "");
                        text_death_date.setText("截至时间：" + detailData.finishTime);

                        text_desc.setText(detailData.description);

                        if (detailData.project != null) {
                            text_project.setText("所属项目：" + detailData.project.name);
                        } else {
                            text_project.setText("所属项目：" + "");
                        }

                        if (detailData.user != null) {
                            text_fzr.setText(detailData.user.userName);
                        } else {
                            text_fzr.setText("");
                        }

                        if (detailData.status.equals("1")) {
                            text_state.setText("未完成");

                            findViewById(R.id.layout_feedback).setVisibility(View.VISIBLE);
                            findViewById(R.id.text_feedback).setVisibility(View.VISIBLE);
                            text_content.setVisibility(View.GONE);
                        } else {
                            text_state.setText("已完成");

                            findViewById(R.id.layout_feedback).setVisibility(View.GONE);
                            findViewById(R.id.text_feedback).setVisibility(View.GONE);

                            text_content.setVisibility(View.VISIBLE);
                            text_content.setText(detailData.contant);
                        }

                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(TaskDetailActivity.this);
                    }
                }
            }
        });
    }

    private String getProjectTypeStr(String type) {
        if (type.equals("2")) {
            return "指派任务";
        } else if (type.equals("1")) {
            return "核实处置任务";
        } else if (type.equals("3")) {
            return "计划任务";
        } else {
            return "";
        }
    }

}
